import UIService from "../../services/uiService";
import { mgrs, sers } from "../../system/OverallSystem ";
import PageBase from "./pageBase";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Dialog extends PageBase {

    public pageName: string = "对话框";

    @property({ type: cc.Label, displayName: "标题" })
    private title: cc.Label = null;

    @property({ type: cc.Label, displayName: "内容" })
    private message: cc.Label = null;

    @property({ type: cc.Node, displayName: "确认按钮" })
    private confirmBtn: cc.Node = null;

    private videoAdCB: () => void = null;

    private isVideoAd: boolean = false;
    private videoAdCnt: number = 0;
    private videoAdLookCnt: number = 0;
    private lookVideoSource:string = "";

    onLoad() {

    }

    start() {
        this._init();
    }

    setDate(message: string, title: string = "温馨提示",
        colseDoFun?: () => void,
        isVideoAd: boolean = false, videoAdCnt: number = 0,
        videoAdCB?: () => void, lookVideoSource?: string) {
        this.title.string = title;
        this.message.string = message;

        this.colseDoFun = colseDoFun;

        this.isVideoAd = isVideoAd;
        this.videoAdCnt = videoAdCnt;
        this.videoAdCB = videoAdCB;
        this.lookVideoSource = lookVideoSource;
    }

    protected _init(): void {
        const btnLayout: cc.Node = cc.find("showLayout/btnLayout", this.showNode);
        const btnX: cc.Node = cc.find("showLayout/panel/btnX", this.showNode);

        btnX.active = !this.isVideoAd;
        btnLayout.active = this.isVideoAd;

        if (this.isVideoAd) {
            const cnt: cc.Label = this.confirmBtn.getChildByName("cnt").getComponent(cc.Label);
            if (this.videoAdCnt > 1) {
                cnt.node.active = true;
                this.updateVideoCnt();
            } else {
                cnt.node.active = false;
            }
        }

        this.open();
    }

    open(): void {
        UIService.openPageBasicAni(this.showNode, true)
            .then(() => {

            });
    }

    close(): void {
        this.closeAni().then(() => {
            sers().uiSrv.deletePageRecord(this.id);
        });
    }

    OnBtnClose(): void {
        mgrs().soundMgr.playBtnSound();
        this.closeAni().then(() => {
            this.colseDoFun && this.colseDoFun();

            sers().uiSrv.deletePageRecord(this.id);
        });
    }

    OnBtnConfirm() {
        mgrs().soundMgr.playBtnSound();
        if (this.isVideoAd) {
            sers().platformSrv.platformApi.loadAndShowVideoAd(() => {
                //FIX 
                this.videoAdLookCnt++;
                this.updateVideoCnt();
                if (this.videoAdLookCnt >= this.videoAdCnt) {
                    this.closeAni().then(() => {
                        sers().uiSrv.deletePageRecord(this.id);
                        this.videoAdCB && this.videoAdCB();
                    });
                }
            }, (err) => {
                console.error(err);
            }, null, this.lookVideoSource);
        }
    }

    protected closeAni(isResume: boolean = true) {
        return UIService.closePageBasicAni(this.showNode, isResume);
    }

    private updateVideoCnt() {
        if (this.videoAdCnt > 1) {
            const cnt: cc.Label = this.confirmBtn.getChildByName("cnt").getComponent(cc.Label);
            cnt.string = `( ${this.videoAdLookCnt} / ${this.videoAdCnt} )`;
        }
    }

    update(dt) {

    }
}
